package com.maxd.modules.chat.controller;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.chat.entity.Chat;
import com.maxd.modules.chat.service.ChatService;
import com.maxd.respository.userinfo.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value="聊天会话",tags={"聊天会话"})
@RequestMapping(value = "/chat")
public class ChatController {
    @Autowired
    private ChatService service;

    @IgnoreSecurity
    @GetMapping("/count")
    @ApiOperation("商家端未读消息总数")
    public Result count(@ApiParam("店铺id(总后台商户传0)") @RequestParam Long storeId) {
        return service.count(storeId);
    }

    @IgnoreSecurity
    @GetMapping("/userCount")
    @ApiOperation("用户端未读消息总数")
    public Result userCount(@RequestParam String userId) {
        return service.userCount(userId);
    }

    @IgnoreSecurity
    @GetMapping("/list")
    @ApiOperation("商家端会话列表")
    public Result findAll(@ApiParam("店铺id(总后台商户传0)") @RequestParam Long storeId,
                          @ApiParam("用户昵称") @RequestParam(required = false) String userName) {
        return service.findAll(storeId, userName);
    }

    @IgnoreSecurity
    @GetMapping("/userList")
    @ApiOperation("用户端会话列表")
    public Result userList(String userId, @ApiParam("店铺名称") @RequestParam(required = false) String storeName) {
        return service.userList(userId, storeName);
    }

    @IgnoreSecurity
    @GetMapping("/find")
    @ApiOperation("查询")
    public Result findOne(Long id) {
        return service.findOne(id);
    }

    @IgnoreSecurity
    @PostMapping("/save")
    @ApiOperation("用户端发起聊天")
    public Result saveBody(@RequestBody Chat entity) {
        return service.saveBody(entity);
    }

    @IgnoreSecurity
    @PostMapping("/update")
    @ApiOperation("修改")
    public Result updateBody(@RequestBody Chat entity) {
        return service.updateBody(entity);
    }

    @GetMapping("/delete")
    @ApiOperation("删除聊天会话")
    public Result delete(Long id) {
        return service.delete(id);
    }

    @Autowired
    private UserRepository userJpaRepository;
    @GetMapping(value = "/getUserByInvitationCode")
    @ApiOperation("用户端和管理平台获取用户详情")
    public Result getUserByInvitationCode(String invitationCode) {
        return ResultUtil.success(userJpaRepository.getUserByInvitationCode(invitationCode));
    }
}
